<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="description" content="">
  <meta name="keywords" content="">
  <meta name="viewport"
        content="width=device-width, initial-scale=1">
  <title>添加商品</title>

  <!-- Set render engine for 360 browser -->
  <meta name="renderer" content="webkit">

  <!-- No Baidu Siteapp-->
  <meta http-equiv="Cache-Control" content="no-siteapp"/>

  <link rel="shortcut icon" href="favicon.ico" /> 

  <!-- Add to homescreen for Chrome on Android -->
  <meta name="mobile-web-app-capable" content="yes">

  <!-- Add to homescreen for Safari on iOS -->
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta name="apple-mobile-web-app-title" content="Amaze UI"/>

  <link rel="stylesheet" href="assets/css/amazeui.min.css">
  <link rel="stylesheet" href="assets/css/app.css">
  <link href="https://cdn.bootcss.com/Swiper/4.3.0/css/swiper.min.css" rel="stylesheet">

  <?php
    
    
      session_start();
      error_reporting(0);
      require('inc/func.php');
      require('inc/mysql.php');     
          
      /*if($_GET['out']==true){
        session_destroy();
        redirect('','redirect','login/index.php');
        exit();
      }

      if(empty($_SESSION['loginUserName'])){redirect('','redirect','login/index.php');}
      $db->query('select * from adminUser where username=\''.$_SESSION['loginUserName'].'\' and password=\''.$_SESSION['loginPassword'].'\' limit 1');
        if($db->num_rows()<0){redirect('非法访问请登录！','redirect','login/index.php');exit();}
        $adminUser=$db->query_first('select * from adminUser where id=1 limit 1');
        $adminUsername=$adminUser['username'];*/
      
      $storeid=!empty($_GET['store_id'])?$_GET['store_id']:redirect('商户参数错误','close');
      if($_GET['action']=='save'){
        /*$db->query("insert into hjmall_goods set store_id=1");
        die;*/
        $num=$_POST['num'];
        $delete=$_POST['delete'];
        $good_arr[0]=array(
          'attr_list'=>array(
                   0=>array(
                 "attr_id"=>"",
                 "attr_name"=>""
              )
                 ),
          'num'=>$num,
          "price"=>'',
          "no"=>"",
          "pic"=>""
                );
        $good_attr=json_encode($good_arr);
        $imgname2=$_POST['imgname2'];
        $goods_group=$_POST['goods_group']?:0;
        $goods_name=$_POST['goods_name'];
        $goods_price=$_POST['goods_price'];
        // $goods_number=$_POST['goods_number'];
        //$goods_cun=$_POST['goods_cun'];
        $goods_detail=$_POST['goods_detail'];
        $imgname=$_POST['imgname'];
        $imgnameHouse=$_POST['imgnameHouse'];
        $agent_first = $_POST['agent_first'];
        $agent_second = $_POST['agent_second'];
        $agent_third = $_POST['agent_third'];

        $goods_group=!empty($goods_group)?$goods_group:redirect('未选择分类123','redirect','index.php?store_id='.$storeid);
        $goods_name=!empty($goods_name)?$goods_name:redirect('名字不能为空','redirect','index.php?store_id='.$storeid);
        if($goods_price<=0){redirect('价格不能为空','redirect','index.php?store_id='.$storeid);}
        // $goods_number=!empty($goods_number)?$goods_number:redirect('货号不能为空','redirect','index.php?store_id='.$storeid);
        $goods_detail=!empty($goods_detail)?$goods_detail:redirect('商品详情不能为空','redirect','index.php?store_id='.$storeid);
        $db->query('insert into hjmall_goods set store_id='.$storeid.',name=\''.$goods_name.'\',price='.$goods_price.',detail=\''.$goods_detail.'\',cover_pic=\''.$imgname.'\',video_url=\''.$imgnameHouse.'\',cat_id='.$goods_group.',sort=1000,addtime='.time().',attr=\''.$good_attr.'\',status='.$delete.',agent_mode=1,agent_money_first='.$agent_first.',agent_money_second='.$agent_second.',agent_money_third='.$agent_third);
          $good_id=$db->insert_id();
        if(!empty($imgname2)){
          $imgPic=explode(',',$imgname2);
          foreach($imgPic as $key=>$val){
            if(!empty($val)){
              $db->query('insert into hjmall_goods_pic set goods_id='.$good_id.',pic_url=\''.$val.'\'');
            }
          }
        }
        redirect('添加成功','redirect','index.php?store_id='.$storeid);
      }
    ?>
  <style>
    .am-slider-default .am-control-nav{bottom: 15px;}
    .upload p{border: 1px solid #e5e5e5;width: 100px;height: 100px;margin: 0 auto;text-align: center;}
    .pic_upload{ position: relative;}
    .change{position: absolute;overflow: hidden;right: 0;top: 0;opacity: 0;}
    #pic_lun img{height:225px;}
    #single_pic,#feedback2,#feedback3{position:relative;text-align:center;background:#fff;}
    #single_pic .imgClose,#feedback2 .imgClose,#feedback3 .imgClose{width:20px;height:20px;position:absolute;right:0px;top:0px;cursor:pointer;padding:0px;border:0px;border-radius:0px;}

  </style>
</head>
<body>
  <div id="lunbo" style="position: relative;background: #e5e5e5;">
    <div class="swiper-container" style="width:100%;height:225px;">
      <div class="swiper-wrapper" id="pic_lun">
      </div>
      <a class="am-btn am-btn-warning" href="#" style="position: absolute;bottom: 20px;left: 20px;z-index: 6;border-radius: 50px;">
        <i class="am-icon-picture-o"></i>上传
        <input class="change imgfile2" name="imgfile2" type="file" multiple="multiple" />
      </a>
    </div>
    <!-- <div class="swiper-pagination"></div>
    <div class="swiper-button-next"></div>
    <div class="swiper-button-prev"></div> -->
  </div>
<div class="am-container">
    <div class="am-g doc-am-g">
        <form action="?store_id=<?php echo $storeid;?>&action=save" method="post" enctype="multipart/form-data" class="am-form am-form-horizontal">
            <div class="am-form-group">
              <label for="doc-vld-name" class="am-u-sm-3 am-form-label">标题</label>
              <div class="am-u-sm-9">
                <input type="text" style="border: none;" name="goods_name" id="goods_name" maxlength="15" placeholder="(必填)不超过15个字" class="am-form-field" required/>
            </div>
            <br>
            <hr>
            <div class="am-form-group">
              <label for="doc-vld-name"  class="am-u-sm-3 am-form-label">分类</label>
              <div class="am-u-sm-9">
                  <select multiple data-am-selected name="goods_group" id="goods_group">
                    <?php 
                      $db->query('select * from hjmall_cat where parent_id=0 and store_id='.$storeid);
                      while($row=$db->next_record()){ ?>
                      <option value="<?php echo $db->f('id');?>"><?php echo $db->f('name');?></option>
                       <?php
                         $id_arr[]=$row;
                        }
                        foreach($id_arr as $val){
                           $db->query('select * from hjmall_cat where parent_id='.$val['id'].' and store_id='.$storeid);
                           while($db->next_record()){
                      ?>
                      <option value="<?php echo $db->f('id');?>"><?php echo $val['name'].'-'.$db->f('name');?></option>
                       <?php }?>
                    <?php }?>
                  </select>
              </div>
            </div>
            <hr>
            <p style="width: 100%;height: 20px;margin:0px;"></p>
            <div class="upload" style="overflow: auto;">
                <div class="am-u-sm-6 pic_upload" id="single_pic">
					       <p class="up_imgfile1">
                  图片缩略图<br>
						      <input style="position: absolute;overflow: hidden;width:100px;height:100px;left: 0;top: 0;opacity: 0;" type="file"  name="imgfile" id="imgfile" multiple="multiple"/>（325*325）
    					   </p>
        				</div>
                <div class="am-u-sm-6 pic_upload" style="width:100px;height:100px;">
        					<p class="up_video">
                    商品视频<br>(大小2M)
                    <input style="position: absolute;overflow: hidden;width:100px;height:100px;right: 0;top: 0;opacity: 0;" type="file"  name="imgHouse" id="imgHouse"  multiple="multiple" />（325*325）
                  </p>
                  <!-- <img src="http://yu.too8tu.top/houseimg/201807/520180716095814.jpg?sample.jpg?x-oss-process=style/stylename" /> -->
        				</div>
            <!-- </div> -->
            <p style="width: 100%;height: 20px;clear:both;border:none;"></p>

            <div class="am-form-group">
              <label for="doc-ipt-3" class="am-u-sm-3 am-form-label">价格</label>
              <div class="am-u-sm-9">
                <input type="number" style="border: none;" id="doc-vld-name" placeholder="请输入价格" name='goods_price' class="am-form-field" required/>
              </div>
            </div>

            <hr>
            <div class="am-form-group">
              <label for="doc-ipt-3" class="am-u-sm-3 am-form-label">库存</label>
              <div class="am-u-sm-9">
                <input type="number" style="border: none;" id="doc-vld-name"  placeholder="请输入库存" name='num' class="am-form-field" required/>
              </div>
            </div>
            <hr>
            <div class="am-form-group">
              <label for="doc-ipt-3" class="am-u-sm-3 am-form-label">高级代理</label>
              <div class="am-u-sm-9">
                <input type="number" style="border: none;" id="doc-vld-name" name="agent_first" placeholder="请输入代理价格" class="am-form-field" required/>
              </div>
            </div>
            <hr>
            <div class="am-form-group">
              <label for="doc-ipt-3" class="am-u-sm-3 am-form-label">产品代理</label>
              <div class="am-u-sm-9">
                <input type="number" style="border: none;" id="doc-vld-name" name="agent_second" placeholder="请输入代理价格" class="am-form-field" required/>
              </div>
            </div>
            <hr>
            <div class="am-form-group">
              <label for="doc-ipt-3" class="am-u-sm-3 am-form-label">商城客服代理</label>
              <div class="am-u-sm-9">
                <input type="number" style="border: none;" id="doc-vld-name" name="agent_third" placeholder="请输入代理价格" class="am-form-field" required/>
              </div>
            </div>
              <hr>
             <div class="am-form-group">
              <label for="doc-vld-ta-2" class="am-u-sm-3 am-form-label">商品详情</label>
              <div class="am-u-sm-9">
                  <textarea id="doc-vld-ta-2" name="goods_detail" rows="6" minlength="10" maxlength="100"></textarea>
              </div>
            </div>
             <div data-am-widget="navbar" class="am-navbar am-cf am-navbar-default am-no-layout" id="">
              <input type="hidden" name="imgname" id="imgname" hidden/>
              <input type="hidden" name="imgname2" id="imgname2" />
              <input type="hidden" name="imgnameHouse" id="imgnameHouse" hidden/>
              <input type="hidden" name="delete" id = "delete" />
              <ul class="am-navbar-nav am-cf am-avg-sm-4"  style="background:none;padding: 0">
                <li><a href="javascript:;" class="am-btn am-btn-warning doDelete">保存仓库</a></li>
                <li><a href="javascript:;" class="am-btn am-btn-danger doSave">立即发布</a></li>
              </ul>
          </div>
        </form>
    </div>
</div>
  <!--在这里编写你的代码-->

  <!--[if (gte IE 9)|!(IE)]><!-->
  <script src="./assets/js/jquery.min.js"></script>
  <!--<![endif]-->
  <!--[if lte IE 8 ]>
  <script src="./assets/js/jquery1.11.3.min.js"></script>
  <script src="./assets/js/modernizr2.8.3.js"></script>
  <script src="./assets/js/amazeui.ie8polyfill.min.js"></script>
  <![endif]-->
  <script src="./assets/js/amazeui.min.js"></script>
  <script src="./assets/js/swiper.min.js"></script>
  <script type="text/javascript">
    var swiper = new Swiper('.swiper-container', {
      // navigation: {
      //   nextEl: '.swiper-button-next',
      //   prevEl: '.swiper-button-prev',
      // }
    });
    $(document).ready(function(){
      var host_url = "<?php echo 'http://' . $_SERVER['SERVER_NAME'] . '/admin/' ?>";
      $('#houseAdmin').find('.xiala').click(function(){
        $(this).find('.xialaContent').css('display','block');
      })
      $("#imgfile").change(function(){
        var data = new FormData();
        $.each($('#imgfile')[0].files, function(i, file) {
          data.append('upload_file'+i, file);
        });
        $.ajax({
          url:'up.php',
          type:'POST', 
          data:data,
          cache: false,
          contentType: false,
          processData: false,
          success:function(data){
            console.log(data);
            if(data!=''){
              var dataarr = data.split(',');
              if(dataarr.length > 2){
                alert("只能上传一张图片");
                return;
              }
              for(var i = 0; i < dataarr.length; i++ ){
                var img_url = dataarr[i];
                if(img_url != ''){
                  $('#clear').remove();
                  $('.up_imgfile1').hide();
                  $("#single_pic").append('<p class="appendP"><img class="phpimg" style="height:100px;wigth:100px;" src='+img_url+' title=""/><img src="inc/images/close.png" alt="" class="imgClose"></p>');
                  $("#single_pic").find('.imgClose').css('visibility','visible');
                  $('#imgname').val(img_url);
                  $('#single_pic').find('.file').hide()
                  $('#single_pic').find('.imgClose').click(function(){
                    $(this).parent('.appendP').remove();
                    $('#single_pic').find('.file').show();
                    $('.up_imgfile1').show();
                  })  
                }
              }
            }
          },
          error:function(){
            alert('上传出错');
          }
        });
      });
      
      $(".imgfile2").change(function(){
        var data = new FormData();
        $.each($('.imgfile2')[0].files, function(i, file) {
          data.append('upload_file'+i, file);
        });
        $.ajax({
          url:'up.php',
          type:'POST', 
          data:data,
          cache: false,
          contentType: false,
          processData: false,
          success:function(data){
            if(data!=''){
              var dataarr = data.split(',');
              for(var i = 0; i < dataarr.length; i++ ){
                var img_url = dataarr[i];
                if(img_url != ''){
                  $('#imgname2').val($('#imgname2').val()+img_url+',');
                  var li = $('<div class="swiper-slide"><img style="width:100%;height:224px" src="'+img_url+'" /></div>');
                  $("#pic_lun").append(li);
                  var mySwiper = new Swiper('.swiper-container', {
                    // navigation: {
                    //   nextEl: '.swiper-button-next',
                    //   prevEl: '.swiper-button-prev',
                    // }
                  });
                }
              }
            }
          },
          error:function(){
            alert('上传出错');
          }
        });
      });
      
      
      $("#imgHouse").change(function(){
        var data = new FormData();
        $.each($('#imgHouse')[0].files, function(i, file) {
          data.append('upload_file'+i, file);
        });
        $.ajax({
          url:'upm.php',
          type:'POST', 
          data:data,
          cache: false,
          contentType: false,
          processData: false,
          success:function(data){
            if(data!=''){
              $('#clear2').remove();
              $("#feedback2").append('<p class="appendP" style="width:120px;"><img class="phpimg" src='+data+' title="视频已上传" alt="视频已上传"/><img src="inc/images/close.png" alt="" class="imgClose"></p><div style="clear:both" id="clear2"></div>');
              $("#feedback2").find('.imgClose').css('visibility','visible');
              $('#imgnameHouse').val(host_url+data);
              $('#feedback2').find('.file').hide()
              $('#feedback2').find('.imgClose').click(function(){
                $(this).parent('.appendP').remove();
                $('#feedback2').find('.file').show()
              })
            }
          },
          error:function(){
            alert('上传出错');
          }
        });
      });
      
      $(".doDelete").click(function(){
        $('input[name="delete"]').val(0);
        $('form').submit();
      })
      $('.doSave').click(function(){
        $('input[name="delete"]').val(1);
        $('form').submit();

      })
    });
  </script>
</body>
</html>